/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.classes.excel;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springblade.common.constant.RedisKeyConstant;
import org.springblade.core.excel.support.ExcelImporter;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.log.entity.IntegralTrainingLogEntity;
import org.springblade.modules.log.service.IIntegralTrainingLogService;
import org.springblade.modules.personal.entity.UserBaseEntity;
import org.springblade.modules.personal.service.IUserBaseService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 年度计划数据导入类
 *
 * @author junxin
 */
@RequiredArgsConstructor
public class TrainingPlanYearImporter implements ExcelImporter<TrainingYearExcel> {

	private final BladeRedis bladeRedis;
	private final IIntegralTrainingLogService integralTrainingLogService;
	private final IUserBaseService userBaseService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(List<TrainingYearExcel> dataList) {
		if (CollectionUtil.isNotEmpty(dataList)) {
			//过滤出新台账数据
			List<TrainingYearExcel> newData = dataList.stream().filter(data -> {
				List<IntegralTrainingLogEntity> oldList = integralTrainingLogService.list(Wrappers.<IntegralTrainingLogEntity>query().lambda()
					.eq(IntegralTrainingLogEntity::getTrainingNumber, data.getTrainingNumber())
					.eq(IntegralTrainingLogEntity::getYear, data.getYear())
					.eq(IntegralTrainingLogEntity::getParentId, 0L));
				return Func.isEmpty(oldList);
			}).collect(Collectors.toList());

			String year = DateUtil.format(new Date(), "yyyy");//当前年度
			bladeRedis.set(RedisKeyConstant.TRAINING_PLAN_YEAR + year, dataList);
			String version = DateUtil.format(new Date(), "yyyyMMddHHmmss");//当前版本
			//年度培训数据同步到台账表
			List<IntegralTrainingLogEntity> saveList = newData.stream().map(data -> {
				IntegralTrainingLogEntity integralTrainingLogEntity = Objects.requireNonNull(BeanUtil.copy(data, IntegralTrainingLogEntity.class));
				integralTrainingLogEntity.setParentId(0L);//默认都是主项
				integralTrainingLogEntity.setVersion(version);
				if (Func.isNotEmpty(integralTrainingLogEntity.getManageUser())) {
					UserBaseEntity userBaseEntity = userBaseService.selectByUserName(integralTrainingLogEntity.getManageUser());
					if (Func.isEmpty(userBaseEntity)) throw new ServiceException("未找到此人:" + integralTrainingLogEntity.getManageUser());
				}
				return integralTrainingLogEntity;
			}).collect(Collectors.toList());
			integralTrainingLogService.saveBatch(saveList);
		}
	}


}
